package com.prj.ufdm.webflux.lambda;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import java.util.function.Function;
import java.util.stream.Collectors;

public class StreamCollectTest {

	public static void main(String[] args) {
		collectTest();
	}

	static void collectTest() {

		Activity activityOne = new Activity(1L, "12幢");
		Activity activityTwo = new Activity(6L, "13幢");
		Activity activityThree = new Activity(5L, "1幢");
		Activity activityFour = new Activity(1L, "2幢");
		
		List<Activity> listOne = new ArrayList<>();
		listOne.add(activityOne);
		listOne.add(activityTwo);
		listOne.add(activityThree);
		listOne.add(activityFour);
		listOne.add(activityFour);
		
		List<Activity> listTwo = new ArrayList<>();
		listTwo.add(activityOne);
		listTwo.add(activityTwo);
		
		List<Activity> listThree = new ArrayList<>();
		listThree.add(activityOne);
		listThree.add(activityTwo);
		listThree.add(activityThree);
		
		// TreeSet 去除属性相同的对象
		TreeSet<Activity> set = new TreeSet<>(Comparator.comparing(e -> e.getId()));
		set.addAll(listOne);
		
		System.out.println("\n set:" + set);
		listOne.stream().forEach(e -> e.setName(e.getName()));
		System.out.println("\n listOne:" + listOne);
		
		List<Activity> collect = listOne.stream().filter(listTwo::contains).collect(Collectors.toList());
		System.out.println("\n 取交集 collect:" + collect);
		
		List<Activity> subCollect = listOne.stream().filter(itme -> !listTwo.contains(itme))
				.collect(Collectors.toList());
		System.out.println("\n 取差集 subCollect:" + subCollect);
		
		List<Activity> distinctCollect = listOne.stream().distinct().collect(Collectors.toList());
		System.out.println("\n 集合去重 distinctCollect:" + distinctCollect);
		
		// 过滤元素
		Activity resctivity = listOne.stream().filter(activity -> activity.getId() == 1).findAny().get();
		System.out.println("\n 过滤元素 resctivity:" + resctivity);
		
		Map<Long, Activity> map = listThree.stream()
				.collect(Collectors.toMap(Activity::getId, Function.identity(), (key1, key2) -> key2));
		System.out.println("\n list转Map:" + map);
		
		List<Long> collect1 = map.keySet().stream().collect(Collectors.toList());
		System.out.println("\n Map key 转list:" + collect1);
		List<Activity> collect2 = map.values().stream().collect(Collectors.toList());
		System.out.println("\n Map value 转list:" + collect2);
		
		List<Activity> collect3 = listThree.stream().sorted(Comparator.comparing(Activity::getId))
				.collect(Collectors.toList());
		System.out.println("\n 按照类的属性排序"+collect3);

	}
}
